<template>
	<view  v-if="shopObj.goods_name" class="MaxBox">
		<!-- 轮播图 -->
		<swiper :autoplay='true' indicator-color="#ccc" indicator-active-color="#c00" :circular='true' :indicator-dots="true">
			<swiper-item v-for="(item,index) in shopObj.pics" :key='index'>
				<view><image :src="item.pics_big" mode="widthFix" @click="getWatch(index)"></image></view>
			</swiper-item>
		</swiper>
		<!-- 标题 -->
		<view>
			<view class="TxtColor">￥{{shopObj.goods_price|numto}}</view>
			<view class="titelBox">
				<view class="titelBox_left">{{shopObj.goods_name}}</view>
				<view class="titelBox_right">
					<uni-icons type="star" size="30"></uni-icons>
					<view class="font">收藏</view>
				</view>
			</view>
			<view class="shipping">快递费:包邮{{cartList.length}}</view>
		</view>
		<!-- 详情ye -->
		<view class="detail" v-html="shopObj.goods_introduce"></view>
		<!-- 点击购买 -->
		<view class="purchase">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from "vuex"
	export default {
		data() {
			return {
				shopObj:{},
				   options: [{
				            icon: 'headphones',
				            text: '客服'
				        }, {
				            icon: 'cart',
				            text: '购物车',
				            info: 0
				        }],
				        buttonGroup: [{
				          text: '加入购物车',
				          backgroundColor: '#ff0000',
				          color: '#fff'
				        },
				        {
				          text: '立即购买',
				          backgroundColor: '#ffa200',
				          color: '#fff'
				        }
				        ]
			};
		},
		onLoad(obj) {
			let {goods_id}=obj;
			this.getDetail(goods_id)
		},
		methods:{
			...mapMutations('cart',['setaddShop']),
			getDetail(id){
				uni.$http.get('/api/public/v1/goods/detail?goods_id='+id).then(res=>{
					res.data.message.goods_introduce=res.data.message.goods_introduce.replace(/<img/g,'<img style="display:block;"').replace(/webp/g,'jpg')//正则替换
					this.shopObj=res.data.message
				})
			},
			getWatch(index){
				uni.previewImage({
					current:index,
					urls:this.shopObj.pics.map(v=>v.pics_big)
				})
			},
			 onClick (e) {
				if(e.content.text=="购物车"){
					uni.switchTab({
						url:'../../pages/cart/cart'
					})
				}else{
					console.log('客服')
				}
			
			 },
			buttonClick (e) {
					  if(e.index=='0'){
						let obj={
							goods_id:this.shopObj.goods_id,
							goods_name:this.shopObj.goods_name,
							goods_price:this.shopObj.goods_price,
							goods_count:1,
							goods_small_logo:this.shopObj.goods_small_logo,
							goods_state:true,
						}
						this.setaddShop(obj);
						uni.showToast({
						  title:'已加入购物车!',
						  icon: 'success'
						})
						return
			}
				
					uni.showToast({
					  title:'功能未开发!',
					  icon: 'none'
					})
			      
			      }
	
		},
		filters:{
			numto(num){
				return Number(num).toFixed(2)
			}
		},
		computed:{
			...mapGetters('cart',['getshop'])
		},
		watch:{//监听器
			getshop:{
				handler(newVal){
					const item=this.options.find(x=>x.text=='购物车')//flag假未定义,真返回对象
					item&&(item.info=newVal.num)
				},
				immediate:true,//页面加载时触发
			}
		}
	}
</script>

<style lang="scss">
swiper{height:780rpx;image{height: 100%;width: 100%;}}
.TxtColor{color: red;padding-left:10rpx;}
.titelBox{display: flex;justify-content: space-around;}
.titelBox_left{flex: 5;font-size: 26rpx;padding-left: 10rpx;border-right: 1px #ccc solid;}
.titelBox_right{flex: 1;display: flex;justify-content: space-around;flex-direction: column;align-items: center;}
.font{font-size:24rpx;}
.detail{margin-top:10rpx;}
.purchase{position: sticky;bottom: 0;z-index: 999;}
.shipping{font-size: 24rpx;padding-left: 10rpx;color: #ccc;}
</style>
